/* Copyright (c) 2004-2008, Andriy Nikolov
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.processmining.email;

import javax.swing.table.*;
import java.text.*;

/**
 * <p>A table model to show the list of e-mail messages in a table</p>
 *
 * @author Andriy Nikolov
 * @version 1.0
 */
public class EMailLogTableModel
    extends AbstractTableModel {
  EMailLog eMailLog;

  public String[] columnNames = {
    "No.",
    "From",
    "To",
    "Subject",
    "Delivery date",
    "Included"
  };

  public EMailLogTableModel() {
  }

  public EMailLogTableModel(EMailLog log) {
    eMailLog = log;
  }

  public void setEMailLog(EMailLog log) {
    eMailLog = log;
  }

  public EMailLog getEMailLog() {
    return eMailLog;
  }

  public int getRowCount() {
    return eMailLog.getOrderedMessages().size();
  }

  public int getColumnCount() {
    return columnNames.length;
  }

  public String getColumnName(int col) {
    return columnNames[col];
  }

  public Class getColumnClass(int c) {
    if (c==5)
      return new Boolean(true).getClass();
    return new String().getClass();
  }

  public boolean isCellEditable(int row, int col) {
    if (col==5)
      return true;
    return false;
  }

  public Object getValueAt(int row, int col) {
    EMailMessage message;
    EMailRecipient tmp;
    int i;
    String answer;
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
    message = (EMailMessage)eMailLog.getOrderedMessages().get(row);
    switch (col) {
      case 0:
        return Integer.toString(row+1);
      case 1:
        return message.getFrom().getName();
      case 2:
        answer = "";
        for (i=0;i<message.getTo().size();i++) {
          tmp = (EMailRecipient)message.getTo().get(i);
          if (i>0) {
            answer+=", ";
          }
          answer+=tmp.getName();
        }
        return answer;
      case 3:
        return message.getSubject();
      case 4:
        return sdf.format(message.getSentOn());
      case 5:
        return new Boolean(message.isActive());
    }
    return new String();
  }

  public void setValueAt(Object value, int row, int col) {
    EMailMessage message;
    Boolean active;
    message = (EMailMessage) eMailLog.getOrderedMessages().get(row);
    if (col == 5) {
      active = (Boolean) value;
      message.setActive(active.booleanValue());
    }
  }
}
